from django.db.models.signals import post_save
from django.dispatch import receiver
from django.contrib.auth.models import User
from .models import UserProfile
from game_core.models import Character

@receiver(post_save, sender=User)
def create_user_profile(sender, instance, created, **kwargs):
    if created:
        UserProfile.objects.create(user=instance)
        # 创建初始角色
        Character.objects.create(
            user=instance,
            name=f"{instance.username}的角色",
            level=1,
            gold=0,
            base_hp=100,
            base_attack=10,
            base_defense=5
        )

@receiver(post_save, sender=User)
def save_user_profile(sender, instance, **kwargs):
    try:
        instance.profile.save()
    except UserProfile.DoesNotExist:
        UserProfile.objects.create(user=instance)

# 这里可以添加信号处理器 